<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            border: 1px solid black;
            display: block;
            margin: 10px auto;
        }
    </style>
</head>
<body>
    <canvas class="box" width="600" height="400"></canvas>
</body>
</html>
<script>
    //获取canvas标签
    let cavnas = document.querySelector('canvas');
    //获取上下文[2D]
    let ctx  = cavnas.getContext('2d');
    //绘制线段
    ctx.moveTo(0,0);//起点的设置
    ctx.lineTo(100,100);//其他位置的点的设置
    ctx.lineTo(150,20);
    ctx.lineTo(200,200);
    //线条的宽度
    ctx.lineWidth='10'
    //线段颜色
    ctx.strokeStyle="red"
    ctx.stroke();

</script>